<?php
/*
 * Validation rules
 *
 * @package yasns
 * @subpackage library
 */
class Validator
{	
	
	/**
	 * validate an associative array based on given $validators.
	 * $validators will be an associative array with optional arguments.
	 * example
	 *		$values = array('username' => 'user1', 'password' => 'abc');
	 *		$validators = array('isEmpty' => null, 'isStringLength' => array(null,10))
	 * 
	 * if $error is provided, then it will use the pointer to store error messages
	 * 
	 * if $break is false, it will check all validations
	 * 
	 * @param type $values
	 * @param type $validators
	 * @param type $error
	 * @param type $break 
	 */
	public static function validate($values, $errors = nil)
	{
		$errors = array();

		foreach($values as $key => $data) {
			$value = $data['value'];
			if(isset($data['validators'])) {
				$validators = (array) $data['validators'];
				$emessages = (isset($data['errors'])) ? $data['errors'] : array();

				foreach($validators as $validator => $args) {
					//print_r($validator);
					
					//die();
					if(is_null($args)) {
						$args = array();
					}
					array_unshift($args, $value);
					
					$error = call_user_func("self::{$validator}", $args);
					
					if($error !== FALSE) {
						print "INSDIE";
						if($error) {
							if(isset($emessages[$validator])) {
								$errors[$key][] = $emessages[$validator];
							} else {
								$errors[$key][] = $validator;
							}
						}
					} else {
						// internal error occured
					}
				}
			}
		}
		
		if($errors)	return false;
		else true;
	}
	
	
	/**
	 * check if value is empty
	 * 
	 * @param string $value
	 * @return boolean
	 */
	public static function isEmpty($value) 
	{
		if (empty($value)) { return true;	}
		
		return false;
	}
	
	/**
	 * check if value is NOT empty
	 * if not empty, return true
	 * else, return false
	 * 
	 * @param string $value
	 * @return boolean
	 */
	public static function isNotEmpty($value) 
	{
		if (!empty($value)) { return true; }
		
		return false;
	}
	
	/**
	 * check if value is valid email
	 * @param string $value
	 * @return boolean 
	 */
	public static function isEmail($value)
	{
		if(!filter_var($value, FILTER_VALIDATE_EMAIL)) {
			return false;
		}
		
		return true;
	}

	
	/**
	 * check if value contains only alphabetic characters
	 * @param string $value
	 * @return boolean 
	 */
	public static function isAlpha($value)
	{
		//use a-zA-Z match
		$pattern = '/[^a-zA-Z]/';

		if( $value != preg_replace($pattern, '', (string) $value) ) {
         return false;
		}
      return true;
	}
	
	
	/**
	 * check if value contains only alpha numeric characters
	 * @param string $value
	 * @return boolean 
	 */
	public static function isAlphaNum($value)
	{
		//use alternative a-zA-Z0-9 match
      $pattern = '/[^a-zA-Z0-9]/';

		if( $value != preg_replace($pattern, '', (string) $value) ) {
         return false;
		}
      return true;
	}
	
	/**
	 * check if the value matches the length requirement
	 * 
	 * if both min and max available, check for range
	 * if only min available, check for less than
	 * if only max available, check for more than
	 * 
	 * @param string $fieldname
	 * @param string $value
	 * @param int $min
	 * @param int $max
	 * @return string
	 */
	public static function isStringLength($value, $min = null, $max = null)
	{	
		// only test for min if min is defined
		if($min !== null) {
			if(strlen($value) < $min) {
				// too short
				return false;
			}
		}
		
		// only test for max if max is defined
		if($max !== null) {
			if(strlen($value) > $max) {
				// too long
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * Compare two values
	 * 
	 * @param string $fieldname1
	 * @param string $fieldname2
	 * @param string $value1
	 * @param string $value2
	 * @return string
	 */
	public static function isEqual($value1, $value2) {
		if( strcmp((string) $value1, (string) $value2) != 0) {
			return false;
      }
		return true;
	}
	
	/**
	 * check if value is a number
	 * 
	 * @param int $value
	 * @return boolean 
	 */
	public static function isNumber($value) {
		if (!is_numeric($value)) {
			return false;
		}
		return true;
	}
}
?>
